package mobile.persistencia;

import mobile.dominio.Cliente;
import mobile.dominio.LineaVenta;
import mobile.dominio.Parametros;
import mobile.dominio.SaldoEnvase;
import mobile.dominio.Venta;
import moio.util.ArrayList;
import waba.io.Catalog;
import waba.io.DataStream;
import waba.io.IOError;
import waba.io.ResizeRecord;
import waba.sys.Settings;

public class PLineaVenta extends Catalog {
	private ResizeRecord rs;
	private DataStream ds;

	public static PLineaVenta instance = new PLineaVenta();

	/**
	 * Constructor which creates or open ( if it already exists ) the Customers.pdb file,
	 * attach the ResizeRecord and DataStream. The pragmatic way of reading/writing to a
	 * Catalog is by attaching those streams (ResizeRecord/DataStream) so that the resizing
	 * of the Catalog and information writing/reading are performed by those classes
	 */
	public PLineaVenta(){
		super("LineaVenta.mGqv.DATA", Catalog.CREATE);
		//super("LineaVenta."+ Settings.appCreatorId +".DATA", Catalog.CREATE);
		rs = new ResizeRecord(this, 128);
		ds = new DataStream(rs);
	}

	public void write(LineaVenta unaLineaVenta, boolean update)
	{
		try {
			if (update){
				int n = indexOf(unaLineaVenta);
				if (n == -1 || !rs.restartRecord(n)) 
					return;
			}else{
				rs.startRecord(); /* start the record at the next available position */
			}

			unaLineaVenta.saveTo(ds);
			rs.endRecord(); /* finishes the writing of a record */
		} catch (IOError e) {}
	}
	
	/**
	 * Removes a record ( Customer ) from the pdb file
	 * 
	 * @param cli
	 *           the Customer
	 */
	public void remove(LineaVenta unaLineaVenta){
		/*
		 * We must set the record position to the record we want to delete, and then delete
		 * the record.
		 */
		int n = indexOf(unaLineaVenta);
		try {
			if (n >= 0 && this.setRecordPos(n)){
				this.deleteRecord();
			}
		} catch (IOError e) {}
	}
	

	/**
	 * Find the index ( position ) of the record ( Customer ) inside the pdb file
	 * 
	 * @param cli
	 *           the Customer
	 * @return the index position or -1 if the Customer was not found on the record
	 */
	public int indexOf(LineaVenta unaLineaVenta){
		for (int n = this.getRecordCount() - 1; n >= 0; n--)
			try {
				if (this.setRecordPos(n)) {
					LineaVenta laLineaVenta = read();

					if (laLineaVenta.getId() == unaLineaVenta.getId() && laLineaVenta.getIdVenta() == unaLineaVenta.getIdVenta()){
						return n;
					}
				}
			} catch (IOError e) {

			}
			return -1;
	}
	

	/**
	 * Reads a record ( Customer ) and construct a Customer. Notice that the order in which
	 * you read informations from the Customer is relevant.
	 * 
	 * @return the Customer
	 */
	public LineaVenta read()
	{
		return new LineaVenta(ds);
	}
	

	public void lineaVentaXVenta(Venta unaVenta){

		int n = this.getRecordCount();

		for (int i = 0; i < n; i++){
			try {
				if (this.setRecordPos(i)){
					LineaVenta unaLineaVenta = read();
					if(unaLineaVenta.getIdVenta() == unaVenta.getId()){
						unaVenta.getColLineas().add(unaLineaVenta);
					}
				}
			} catch (IOError e) {

			}
		}
	}
	
	public void eliminarLineas(long idVenta){
		int n = this.getRecordCount();

		for (int i = 0; i < n; i++){
			try {
				if (this.setRecordPos(i)){
					LineaVenta unaLineaVenta = read();
					if(unaLineaVenta.getIdVenta() == idVenta){
						remove(unaLineaVenta);
					}
				}
			} catch (IOError e) {

			}
		}
	}
}
